package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzx.mapper.nydsj.BjxxMapper;
import com.lzx.model.User;
import com.lzx.model.jzny.Jzny;
import com.lzx.model.nydsj.*;
import com.lzx.model.qxdata.QxData;
import com.lzx.model.wlwjd.Land;
import com.lzx.service.UserService;
import com.lzx.service.nydsj.*;
import com.lzx.service.qxdata.QxDataService;
import com.lzx.service.wlwjd.LandService;
import com.lzx.utils.ConnectionUtil;
import com.lzx.utils.SessionUtil;
import com.lzx.utils.StringUtils;
import com.lzx.utils.WeatherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sy/nydsj")
public class NydsjController {
    @Autowired
    private LsjgService lsjgService;
    @Autowired
    private SdclService sdclService;
    @Autowired
    private TrphzService trphzService;
    @Autowired
    private BjxxService bjxxService;
    @Autowired
    private ZzmjService zzmjService;
    @Autowired
    private HfService hfService;
    @Autowired
    private YxjService yxjService;
    @Autowired
    private YxlService yxlService;
    @Autowired
    private ZhsjService zhsjService;
    @Autowired
    private TryjzService tryjzService;
    @Autowired
    private UserService userService;
    @Autowired
    private QxDataService qxDataService;
    @Autowired
    private LandService landService;


    //User userD = new User();

    //跳转农业大数据
    @RequestMapping("/nydsj.html")
    public ModelAndView nydsj(HttpServletRequest request) throws SQLException {
        ModelAndView mv = new ModelAndView("system/nydsj");
        User user= SessionUtil.getUser(request);
        String name =user.getUsername();
        String address = request.getParameter("address");
        String countyId = "";//取市县ID

        if(StringUtils.isNotEmpty(address)){
            SessionUtil.setAttr(request,"address",address);
            User user1 = userService.queryByUserName(address);
            countyId = user1.getUserId();
        }else{
            if(user.getIsAdmin().equals("Y")){
                User user1 = userService.queryByUserName((String)SessionUtil.getAttr(request,"address"));
                countyId = user1.getUserId();
            }else{
                countyId = user.getUserId();
            }
        }


        SessionUtil.setAttr(request,"nydsjcountyId",countyId);

        System.out.println("countyId---------"+countyId);

        Land land=new Land();
        land.setCountyId(countyId);
        List<Land> lanlist=landService.findland(land);


        if(user.getIsAdmin().equals("Y")){
            mv.addObject("address",SessionUtil.getAttr(request,"address"));
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("address",name);
            mv.addObject("countyId",countyId);//往页面添加市县id
            mv.addObject("isAdmn","FALSE");
        }
        mv.addObject("lanlist",lanlist);
        return mv;
    }

    //粮食价格块
    @RequestMapping("/lsjg")
    public String lsjg(HttpServletRequest request) {
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Lsjg lsjg = new Lsjg();
        lsjg.setCountyId(countyId);
        //List<Lsjg> list = lsjgService.findlsjg(lsjg);
        List<Lsjg> list = lsjgService.findLsjgAvg(lsjg);
        List<Lsjg> lsjgListGROUP = lsjgService.findLsjgListGROUP(lsjg);//查品类
        String s = JSON.toJSONString(list);
        String s1 = JSON.toJSONString(lsjgListGROUP);
        JSONObject jsonstr = new JSONObject();
        jsonstr.put("name", s1);
        jsonstr.put("data", s);
        return jsonstr.toJSONString();
    }
    //水稻产量块
    @RequestMapping("/sdcl")
    public String sdcl(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Sdcl sdcl = new Sdcl();
        sdcl.setCountyId(countyId);
        List<Sdcl> list =sdclService.findlist(sdcl);
        List<Sdcl> sdclListGroup = sdclService.findlistGroup(sdcl);
        String s = JSON.toJSONString(list);
        String s1 = JSON.toJSONString(sdclListGroup);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",s1);
        jsonObject.put("data",s);
        return jsonObject.toJSONString();
    }
    //土壤PH值块
    @RequestMapping("/trphz")
    public String trphz(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");


        Trphz trphz = new Trphz();
        trphz.setCountyId(countyId);
        List<Trphz> list = trphzService.findList(trphz);
        return JSON.toJSONString(list);
    }
    //报警信息块
    @RequestMapping("/bjxx")
    public String bjxx(HttpServletRequest request){
        String city_code="";
        User user = SessionUtil.getUser(request);
        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");
        String username="";
        if(user.getIsAdmin().equals("Y")){
            String address = (String)SessionUtil.getAttr(request,"address");
            User user1 = null;
            try {
                user1 = userService.queryByUserName(address);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //username = userD.getRealname();
            username = user1.getRealname();
        }else{
            username=user.getRealname();
        }

        String city_name=username;
        //接登陆账号的username  按username比对城市天气代码 按代码取接口查天气数据
        if(city_name.equals("绥滨")){
            city_code="101051202";
        }else if(city_name.equals("萝北")){
            city_code="101051203";
        }else if(city_name.equals("七台河")){
            city_code="101051002";
        }else if(city_name.equals("孙吴")){
            city_code="101050603";
        }else if(city_name.equals("饶河")){
            city_code="101051304";
        }else if(city_name.equals("逊克")){
            city_code="101050604";
        }else if(city_name.equals("呼玛")){
            city_code="101050704";
        }else if(city_name.equals("讷河")){
            city_code="101050202";
        }else if(city_name.equals("安达")){
            city_code="101050503";
        }else if(city_name.equals("富锦")){
            city_code="101050407";
        }else if(city_name.equals("双鸭山")){
            city_code="101051301";
        }else{
            city_code="101050101";  //哈尔滨
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        WeatherUtil weatherUtil = new WeatherUtil();
        Map<String,Object> map= weatherUtil.getTodayWeather(city_code,city_name,date);


        Bjxx bjxx = new Bjxx();
        bjxx.setPosition( map.get("tianqi_chengshi").toString());
        bjxx.setCreateDate(map.get("tianqi_createTime").toString());
        bjxx.setSituation("温度预警");
        bjxx.setSituationValue(map.get("tianqi_wendu").toString());
        bjxx.setStandard("20");
        bjxx.setRegion(username);
        bjxx.setCountyId(countyId);
        bjxx.setFx(map.get("tianqi_fengxiang").toString());
        bjxx.setFl(map.get("tianqi_fengsu").toString());
        List<Bjxx> list = new ArrayList<>();
        list.add(bjxx);
        /*List<Bjxx> list = bjxxService.findlist(bjxx);*/
        return JSON.toJSONString(list);
    }
    //种植面积块
    @RequestMapping("/zzmj")
    public String zzmj(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");


        Zzmj zzmj = new Zzmj();
        zzmj.setCountyId(countyId);
        List<Zzmj> list = zzmjService.findList(zzmj);
            String b="";
        for (int i = 0; i <list.size() ; i++) {
            String a="";
             a = list.get(i).getArea();
                for (int j = 0; j <6 ; j++) {
                    if(a.length()<6){
                    a = "0"+a;
                    list.get(i).setArea(a);
                }
                }
        }
        return JSON.toJSONString(list);
    }
    //化肥块
    @RequestMapping("/hf")
    public String hf(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Hf hf = new Hf();
        hf.setCountyId(countyId);
        //List<Hf> list = hfService.findlist(hf);
        List<Hf> list = hfService.findlistByCategory(hf);
        return JSON.toJSONString(list);
    }

    //有效钾块
    @RequestMapping("/yxj")
    public String yxj(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Yxj yxj = new Yxj();
        yxj.setCountyId(countyId);
        List<Yxj> list = yxjService.findList(yxj);
        return JSON.toJSONString(list);
    }
    //有效磷块
    @RequestMapping("/yxl")
    public String yxl(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Yxl yxl = new Yxl();
        yxl.setCountyId(countyId);
        List<Yxl> list = yxlService.findList(yxl);
        return JSON.toJSONString(list);
    }
    //综合数据块
    @RequestMapping("/zhsj")
    public String zhsj(HttpServletRequest request){
        String city_code="";
        User user = SessionUtil.getUser(request);
        String username = user.getUsername();
        String city_name=username;
        //接登陆账号的username  按username比对城市天气代码 按代码取接口查天气数据
        if(city_name.equals("绥滨")){
            city_code="101051202";
        }else if(city_name.equals("萝北")){
            city_code="101051203";
        }else if(city_name.equals("七台河")){
            city_code="101051002";
        }else if(city_name.equals("孙吴")){
            city_code="101050603";
        }else if(city_name.equals("饶河")){
            city_code="101051304";
        }else if(city_name.equals("逊克")){
            city_code="101050604";
        }else if(city_name.equals("呼玛")){
            city_code="101050704";
        }else if(city_name.equals("讷河")){
            city_code="101050202";
        }else if(city_name.equals("安达")){
            city_code="101050503";
        }else if(city_name.equals("富锦")){
            city_code="101050407";
        }else if(city_name.equals("双鸭山")){
            city_code="101051301";
        }else{
            city_code="101050101";  //哈尔滨
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        WeatherUtil weatherUtil = new WeatherUtil();
        Map<String,Object> map= weatherUtil.getTodayWeather(city_code,city_name,date);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

       /* List<Zhsj> list = zhsjService.findList(zhsj);*/
        List<Zhsj> list = new ArrayList<>();
        for (int i = 1; i <13 ; i++) {
            Zhsj zhsj = new Zhsj();
            zhsj.setPosition(user.getRealname());
            zhsj.setCountyId(countyId);
            zhsj.setTemperature(map.get("tianqi_wendu").toString());
            zhsj.setQuality(map.get("tianqi_fengxiang").toString());
            zhsj.setStrength("5"+(i+i));
            zhsj.setHumidity("8"+(i+i));
            zhsj.setRainfall("1"+(i+i));
            zhsj.setCreatedate("2018-"+i);
            list.add(zhsj);
        }
        return JSON.toJSONString(list);
    }
    //土壤有机质块
    @RequestMapping("/tryjz")
    public String tryjz(HttpServletRequest request){
        User user = SessionUtil.getUser(request);

        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");

        Tryjz tryjz = new Tryjz();
        tryjz.setCountyId(countyId);
        List<Tryjz> list = tryjzService.findList(tryjz);
        return JSON.toJSONString(list);
    }

    @RequestMapping("/nydsjnew.html")
    public ModelAndView nydsjnew(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        ModelAndView mv = new ModelAndView("zhny/jianCeShuJu");

        String locale = request.getParameter("locale");
        writeCookie(request,response,"locale",locale);

        User user= SessionUtil.getUser(request);
        String name =user.getUsername();
        String address = request.getParameter("address");
        String countyId = "";//取市县ID

        if(StringUtils.isNotEmpty(address)){
            SessionUtil.setAttr(request,"address",address);
            User user1 = userService.queryByUserName(address);
            countyId = user1.getUserId();
        }else{
            if(user.getIsAdmin().equals("Y")){
                User user1 = userService.queryByUserName((String)SessionUtil.getAttr(request,"address"));
                countyId = user1.getUserId();
            }else{
                countyId = user.getUserId();
            }
        }

        SessionUtil.setAttr(request,"nydsjcountyId",countyId);

        //System.out.println("countyId---------"+countyId);

        Land land=new Land();
        land.setCountyId(countyId);
        List<Land> lanlist=landService.findland(land);


        if(user.getIsAdmin().equals("Y")){
            mv.addObject("address",SessionUtil.getAttr(request,"address"));
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("address",name);
            //mv.addObject("countyId",countyId);//往页面添加市县id
            mv.addObject("isAdmn","FALSE");
        }
        mv.addObject("lanlist",lanlist);
       // mv.addObject("landid","22db9c36025b497caeddb140f8a41d0c");


        Zhsj zhsj=new Zhsj();
        zhsj.setCountyId((String) SessionUtil.getAttr(request,"nydsjcountyId"));
        List<Zhsj> listzhsj=zhsjService.findtenList(zhsj);
        mv.addObject("listzhsj",JSON.toJSONString(listzhsj));

        return mv;
    }

    @RequestMapping("/trwdsd")
    public Object trwdsd(HttpServletRequest request) throws SQLException {

        QxData qxData2=new QxData();
        qxData2.setTablename("tbl_18074919003");
        List<QxData> listqxdata2=qxDataService.findqxsjlist(qxData2);

        return JSON.toJSONString(listqxdata2);
    }

    //土壤PH值块
    @RequestMapping("/trphznew")
    public String trphznew(HttpServletRequest request){
        String countyId = (String)SessionUtil.getAttr(request,"nydsjcountyId");
        Trphz trphz = new Trphz();
        trphz.setCountyId(countyId);
        List<Trphz> list = trphzService.findList(trphz);
        double fz=0;
        int fm=0;
        for(Trphz zz:list){
            fz+=Double.parseDouble(zz.getPh());
            fm++;
        }
//        QxData da=new QxData();
//        List<QxData> dalist=qxDataService.findlist(da);
//        for(QxData aa:dalist){
//            System.out.println(aa.toString());
//        }
        return String.format("%.1f", fz/fm);
    }

    @RequestMapping("/nydsjzhsj")
    public Object nydsjzhsj(HttpServletRequest request) throws SQLException {
        Zhsj zhsj=new Zhsj();
        String countyId= (String) SessionUtil.getAttr(request,"nydsjcountyId");
        zhsj.setCountyId(countyId);
        List<Zhsj> listzhsj=zhsjService.findtenList(zhsj);

        return JSON.toJSONString(listzhsj);
    }



    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
